package cn.snailhd.admin.novel.scheduled;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.snailhd.admin.novel.domain.CrawlerNovel;
import cn.snailhd.admin.novel.service.NovelService;
import cn.snailhd.core.base.domain.PageVo;

@Component
public class ScheduledNovel {
	Log log = LogFactory.getLog(this.getClass());

	public static String receiveMailAccount = "snailhd@qq.com";
	private static String page = "2651795.html";
	private boolean isSend = false;
	
	@Autowired
	NovelService service;
	
	
	
	@Scheduled(cron = "0 0/5 * * * ? ")
	public void crawlerAndSendmail() throws Exception {
		log.debug("开始定时爬取任务。。。");
		long bt = System.currentTimeMillis();
		JSONObject params = new JSONObject();
		params.put("enable", "1");
//		params.put("page", "1");
//		params.put("size", "10000");
		PageVo<CrawlerNovel> pv = service.queryNovelList(params);
		List<CrawlerNovel> cras = pv.getRows();
		for(CrawlerNovel cra : cras) {
			while(cra.parse().next()) {
				service.updateNovel(cra);
				service.addChapter(cra);
				//Mail.send(cra.getEmail(), "ss","《"+cra.getName()+"》  "+ cra.getTitle(), cra.getContent());
			}
		}
		long et = System.currentTimeMillis();

		log.debug("爬取完成。。。耗时："+(et-bt)/1000+"s");
	}
	
	
	
}
